package com.ruoyi.demo.controller.User;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.demo.service.IFriendAppService;
import com.ruoyi.system.domain.Friend;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 好友申请管理 Controller
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/demo/friend/app")
public class FriendAppController extends BaseController {

    @Autowired
    private IFriendAppService IFriendAppService;

    /**
     * 获取收到的好友申请列表
     */
    @GetMapping("/received/{userId}")
    public AjaxResult getReceivedList(@PathVariable("userId") Long userId) {
        try {
            List<Friend> list = IFriendAppService.getReceivedList(userId);
            return success(list);
        } catch (ServiceException e) {
            logger.error("获取好友申请列表失败: {}", e.getMessage());
            return error(e.getMessage());
        } catch (Exception e) {
            logger.error("获取好友申请列表失败", e);
            return error("系统错误，请联系管理员");
        }
    }

    /**
     * 获取所有好友记录（测试用）
     */
    @GetMapping("/list")
    public AjaxResult getAllFriends() {
        try {
            List<Friend> list = IFriendAppService.selectAllFriends();
            return success(list);
        } catch (Exception e) {
            logger.error("获取好友记录失败", e);
            return error("获取好友记录失败：" + e.getMessage());
        }
    }

    /**
     * 同意好友申请
     */
    @PutMapping("/accept/{id}")
    public AjaxResult acceptRequest(@PathVariable Long id) {
        try {
            IFriendAppService.acceptRequest(id);
            return success("已同意好友申请");
        } catch (ServiceException e) {
            logger.error("同意好友申请失败: {}", e.getMessage());
            return error(e.getMessage());
        } catch (Exception e) {
            logger.error("同意好友申请失败", e);
            return error("处理失败，请稍后重试");
        }
    }

    /**
     * 拒绝好友申请
     */
    @PutMapping("/reject/{id}")
    public AjaxResult rejectRequest(@PathVariable Long id) {
        try {
            IFriendAppService.rejectRequest(id);
            return success("已拒绝好友申请");
        } catch (ServiceException e) {
            logger.error("拒绝好友申请失败: {}", e.getMessage());
            return error(e.getMessage());
        } catch (Exception e) {
            logger.error("拒绝好友申请失败", e);
            return error("处理失败，请稍后重试");
        }
    }
}
